/*
 * login.java
 *
 * Created on March 26, 2007, 7:11 AM
 *
 * To change this template, choose Tools | Template Manager
 * and open the template in the editor.
 */

package edu.byu.isys413.actions;

import java.util.Random;
import javax.servlet.http.*;
import edu.byu.isys413.group1E.data.*;
import edu.byu.isys413.group1E.handlers.*;

/**This action takes in the customer email & password, and logs in the customer.
 *
 * @author Kendall Frazier
 */
public class login implements edu.byu.isys413.web.Action {
    
    /**
     * Creates a new instance of login
     */
    public login() {
    }
  
    /** 
     *Processes this method takes in the username and password from the index.jsp.  it checks the username and password
     *to make certain they are correct.  If either is incorrect, the error page is returned for 3 seconds, and then
     *the user is given another chance to atempt a login.  If hte info is correct. home.jsp is returned.
     *
     *@param HttpServletRequest
     *@param HttpServletResponse
     *
     *@return String the name of the jsp to call
     */
    public String process(HttpServletRequest request, HttpServletResponse response) throws Exception {
        HttpSession session = request.getSession();
        
        //read in parameters and create sale RevSrc
        String email = request.getParameter("email");
        String password = request.getParameter("password");        
        
        //create & populate Transaction
        CustomerDAO custDAO = CustomerDAO.getInstance();
        CustomerBO custBO = null;
        try{
            custBO = custDAO.readByEmail(email);
        }catch(Exception e){
            String message = "A Customer couldn't be found with that email";
            request.setAttribute("message", message);
            e.printStackTrace();
            return "error.jsp";
        }
        if(custBO.getCustMembership().validatePwd(password)){
            session.setAttribute("customer", custBO);
            request.setAttribute("customer", custBO);
            return "home.jsp";
        }else{
            String message = "Your Password is incorrect";
            request.setAttribute("message", message);
            return "error.jsp";
        }
        
    }//process
    
    
}//login class
